<template>
  <el-dialog :title="formData.id ? '编辑页面' : '添加页面'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :rules="rules" :model="formData" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor
          ref="myQuillEditor"
          v-model="formData.content"
          size="50%"
          @blur="$refs.form.validateField('content')"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row>
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { addPage, getCount, editPage } from '@/api/model/PageStap'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        content: '',
        id: null
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', tirgger: 'blur' },
          { min: 3, message: '标题最少3个字符', tirgger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 编辑
    async editPage(id) {
      this.formData.id = id
      const res = await getCount(id)
      // console.log(res)
      this.formData = res.data
    },
    // 新增
    async submit() {
      if (this.formData.id) {
        await editPage(this.formData)
        // console.log(res)
      } else {
        await addPage(this.formData)
      }
      // 关闭弹窗
      this.closeFn()
      // 刷新数据
      this.$emit('updateEvent')
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        title: '',
        content: ''
      }
      // 清空校验
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style scoped>
::v-deep .ql-editor {
  min-height: 200px;
}

::v-deep .el-table>tr {
  height: 0px !important;
}
</style>
